﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Terminal.Advanced_AEUC_settings_controls.Settings_AUX;

namespace Terminal.Advanced_AEUC_settings_controls.Scripting
{
    /// <summary>
    /// Send message script action
    /// </summary>
    class ScrActSendMsg : ScrBasicAction
    {
        /// <summary>
        /// Message sending callback
        /// </summary>
        CommLink.CommLink.MessageSend sendFunc;

        /// <summary>
        /// Public constructor for the script action
        /// </summary>
        /// <param name="iDispName">Action display text</param>
        /// <param name="iUsageHelp">Usage help and comments text for the action</param>
        /// <param name="callback">Callback function to be used for sending messages</param>
        public ScrActSendMsg(string iDispName, string iUsageHelp, CommLink.CommLink.MessageSend callback)
            : base(iDispName, iUsageHelp)
        {
            sendFunc = callback;

            usageHelp += "Input params: GenMSG(message name) + GenPayloadStruct(payload struct name)\n";
        }

        /// <summary>
        /// Perform the action using specified parameters.
        /// </summary>
        /// <param name="paramList">List of the input/output parameters for the action</param>
        /// <returns>True if action is succesful</returns>
        public override bool Do(List<object> paramList)
        {
            if (breakPoint)
                WaitUserInput();

            if (paramList.Count > 1 && paramList[0].GetType() == typeof(GenMSG) && paramList[1].GetType() == typeof(GenPayloadStruct))
            {
                GenMSG msgSend = (GenMSG)paramList[0];
                msgSend.payloadStruct = (GenPayloadStruct)paramList[1];

                sendFunc(msgSend.GenerateMessage(null));

                return true;
            }

            return false;
        }

        /// <summary>
        /// Generate C source code for specified parameters
        /// </summary>
        /// <param name="paramlist">List of input/output parameters for the action</param>
        /// <returns>C source string</returns>
        public override string GenerateSourceCode(List<object> paramList)
        {
            string c_source = "Can't generate source - check script file!";

            if (paramList.Count > 1 && paramList[0].GetType() == typeof(GenMSG) && paramList[1].GetType() == typeof(GenPayloadStruct))
            {
                GenMSG msgSend = (GenMSG)paramList[0];
                msgSend.payloadStruct = (GenPayloadStruct)paramList[1];

                c_source = "\t" + msgSend.msgDisplayName + "->pPayload = &" + msgSend.payloadStruct.dispName + ";\n";
                c_source += "\t" + msgSend.msgDisplayName + "->nPayloadSize = " + msgSend.payloadStruct.prepareByteStream().Length.ToString() + ";\n";
                c_source += "\twriteMsg( " + msgSend.msgDisplayName + ", sizeof(systemMessage_t*));\n";
            }

            return c_source;
        }

        /// <summary>
        /// Returns a List containing objects of the parameter list required for the Do function.
        /// </summary>
        /// <returns>List of parameter objects</returns>
        public override List<Type> GetParamList()
        {
            List<Type> paramList = new List<Type>();
            paramList.Add(typeof(GenMSG));
            paramList.Add(typeof(GenPayloadStruct));

            return paramList;
        }

        /// <summary>
        /// Returns a List containing objects of the parameter list required for the GenerateSourceCode function.
        /// </summary>
        /// <returns>List of parameter objects</returns>
        public override List<Type> GetParamListSourceGen()
        {
            List<Type> paramList = new List<Type>();
            paramList.Add(typeof(GenMSG));
            paramList.Add(typeof(GenPayloadStruct));

            return paramList;
        }
    }
}

